package zry.Achievement.Other;

import zry.Search.SearchCriteriaOther;
import zry.Util.DBUtil;
import Connection.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OtherDAOImpl extends DAOBase implements OtherDAO {
    private static final String OTHER_INSERT_SQL = "INSERT INTO other_table(achievement_id,other_name,other_content,supporting) VALUES(?,?,?,?)";
    private static final String OTHER_UPDATE_SQL = "UPDATE other_table SET other_name=?,other_content=?,supporting=? WHERE achievement_id=?";
    private static final String OTHER_SELECT_SQL = "SELECT * FROM other_table WHERE achievement_id=?";
    private static final String OTHER_SELECT_ALL_SQL = "SELECT * FROM other_table";

    @Override
    public boolean addOther(Other other) {
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(OTHER_INSERT_SQL);
            psmt.setString(1, DBUtil.fixSqlFieldValue(other.getA_id()));
            psmt.setString(2, DBUtil.fixSqlFieldValue(other.getO_name()));
            psmt.setString(3, DBUtil.fixSqlFieldValue(other.getO_content()));
            psmt.setString(4, DBUtil.fixSqlFieldValue(other.getO_supporting()));
            psmt.executeUpdate();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    @Override
    public boolean updateOther(Other other) {
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(OTHER_UPDATE_SQL);
            psmt.setString(1, DBUtil.fixSqlFieldValue(other.getO_name()));
            psmt.setString(2, DBUtil.fixSqlFieldValue(other.getO_content()));
            psmt.setString(3, DBUtil.fixSqlFieldValue(other.getO_supporting()));
            psmt.setString(4, DBUtil.fixSqlFieldValue(other.getA_id()));
            psmt.executeUpdate();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    @Override
    public Other getOther(String achievement_id) {
        Other other = null;
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(OTHER_SELECT_SQL);
            psmt.setString(1, achievement_id);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                other = new Other();
                other.setA_id(rs.getString("achievement_id"));
                other.setO_name(rs.getString("other_name"));
                other.setO_content(rs.getString("other_content"));
                other.setO_supporting(rs.getString("supporting"));
            }
            rs.close();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return other;
    }

    public List<Other> getAllOthers() {
        List<Other> others = new ArrayList<>();
        Connection con = null;
        try {
            con = getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(OTHER_SELECT_ALL_SQL);
            while (rs.next()) {
                Other other = new Other();
                other.setA_id(rs.getString("achievement_id"));
                other.setO_name(rs.getString("other_name"));
                other.setO_content(rs.getString("other_content"));
                other.setO_supporting(rs.getString("supporting"));
                others.add(other);
            }
            rs.close();
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return others;
    }

    @Override
    public List<Other> findOthers(SearchCriteriaOther searchCriteriaOther) {
        List<Other> others = new ArrayList<>();
        Connection con = null;
        try {
            // 构建SQL语句
            StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM other_table WHERE 1=1");
            if (searchCriteriaOther.getA_id() != null) {
                sqlBuilder.append(" AND achievement_id LIKE ?");
            }
            if (searchCriteriaOther.getO_name() != null) {
                sqlBuilder.append(" AND other_name LIKE ?");
            }
            if (searchCriteriaOther.getO_content() != null) {
                sqlBuilder.append(" AND other_content LIKE ?");
            }
            if (searchCriteriaOther.getO_supporting() != null) {
                sqlBuilder.append(" AND supporting LIKE ?");
            }
            String sql = sqlBuilder.toString();

            // 执行查询
            con = getConnection();
            PreparedStatement psmt = con.prepareStatement(sql);
            int index = 1;
            if (searchCriteriaOther.getA_id() != null) {
                psmt.setString(index++, searchCriteriaOther.getA_id());
            }
            if (searchCriteriaOther.getO_name() != null) {
                psmt.setString(index++, searchCriteriaOther.getO_name());
            }
            if (searchCriteriaOther.getO_content() != null) {
                psmt.setString(index++, searchCriteriaOther.getO_content());
            }
            if (searchCriteriaOther.getO_supporting() != null) {
                psmt.setString(index++, searchCriteriaOther.getO_supporting());
            }
            ResultSet rs = psmt.executeQuery();
            while (rs.next()) {
                String a_id = rs.getString("achievement_id");
                String o_name = rs.getString("other_name");
                String o_content = rs.getString("other_content");
                String o_supporting = rs.getString("supporting");
                Other other = new Other(a_id, o_name, o_content, o_supporting);
                others.add(other);
            }
            rs.close();
            psmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return others;
    }
}
